<?php
/*
 * Azaria images gallery module
 */
class mod_gallery {

private $System=null;

private $maxPhotosPerAlbum = 200;

private $lang=null;

private $AlbumTable = "mod_gallery_albums";

private $PhotosTable = "mod_gallery_photos";

private $smallX = 150;

private $smallY = 0;

private $largeX = 500;

private $largeY = 0;

function __construct(init & $Azaria){

$this->System=&$Azaria;

$this->System->lang->setModuleLanguage("gallery",array("en","ru","ua"),"en",$this->lang);

/*
 * System Languages loading
 */

}


public function photo(){

if(!isset($_GET["id"]) || !is_numeric($_GET["id"]) ){
    return $this->System->errors->Message($this->lang->get("g17"));
}

$id=$this->System->escapeString($_GET["id"]);

$Photo=$this->System->dbo->select($this->PhotosTable,"title,comment,album_id,created,large","where photo_id='".$id."' limit 1 offset 0",_RESULT_ROW);

if(count($Photo)==0) return $this->System->errors->Message($this->lang->get("g17"));

$Album=$this->System->dbo->select($this->AlbumTable,"name"," where album_id='".$Photo["album_id"]."' limit 1 offset 0",_RESULT_ROW);

$Photo["link"]=$this->System->file->GetLink($Photo["large"]);

/*
 * Seeking for prevoius,next photo
 */

$previous=$this->System->dbo->select($this->PhotosTable,"photo_id","where photo_id < '".$id."' and album_id='".$Photo["album_id"]."' limit 1 offset 0",_RESULT_ROW);
$next=$this->System->dbo->select($this->PhotosTable,"photo_id","where photo_id > '".$id."' and album_id='".$Photo["album_id"]."' limit 1 offset 0",_RESULT_ROW);

$previous=(count($previous)==0)?null:$this->System->base->GetActionLink("gallery-photo","id=".$previous["photo_id"]);
$next=(count($next)==0)?null:$this->System->base->GetActionLink("gallery-photo","id=".$next["photo_id"]);

$usePrevious=($previous==null)?0:1;

$useNext=($next==null)?0:1;


$this->System->base->AddWay("gallery-view",null,$this->lang->get("g1"));

$this->System->base->AddWay("gallery-viewalbum","album=".$Photo["album_id"],$Album["name"]);

return $this->System->view->GetTemplate("gallery","photo",array("photo"=>$Photo,"next"=>$next,"useNext"=>$useNext,"previous"=>$previous,"usePrevious"=>$usePrevious) );
}


public function view(){

$Query = $this->System->dbo->select($this->AlbumTable." as a,".$this->PhotosTable." as b","
a.album_id,a.name,a.created,a.comment,b.small","
where a.album_id=b.album_id group by a.album_id limit 100 offset 0");

if(count($Query)==0){

return $this->System->errors->Message($this->lang->get("g2"));
}

$Images=array();

$i=0;
while($i<count($Query)){

$Images[$Query[$i]["album_id"]]=$this->System->file->GetLink($Query[$i]["small"]);

$i++;
}

return $this->System->view->GetTemplate("gallery","view",array("data"=>&$Query,"count"=>count($Query),"image"=>&$Images)  );


}

public function viewAlbum(){

if(!isset($_GET["album"]) || !is_numeric($_GET["album"]) ){
    return $this->System->errors->Message($this->lang->get("g20"));
}

$AlbumId=$this->System->escapeString($_GET["album"]);

$Album=$this->System->dbo->select($this->AlbumTable,"name,created,comment","where album_id='".$AlbumId."'",_RESULT_ROW);

if(count($Album)==0) return $this->System->errors->Message($this->lang->get("g20"));

/*
 * Seeking for photos
 */

$total=$this->System->dbo->select($this->PhotosTable,"COUNT(*) as count","where album_id='".$AlbumId."'",_RESULT_ROW);

$total=$total["count"];

$this->System->base->AddWay("gallery-view",null,$this->lang->get("g1"));

$this->System->base->AddWay("gallery-viewalbum","album=".$AlbumId,$Album["name"]);

$Photos=$this->System->dbo->select($this->PhotosTable." as a, file as b",
"b.name,a.small,a.photo_id,a.title","where b.id=a.small and a.album_id='".$AlbumId."'");

$Links=array();$i=0;
while($i<count($Photos)){

    $Links[$i]=$this->System->file->GetSRC($Photos[$i]["name"]);

    $i++;
}


return $this->System->view->GetTemplate("gallery","viewalbum",array("name"=>$Album["name"],
    "comment"=>$Album["comment"],"created"=>$Album["created"],"num"=>count($Photos),"photos"=>&$Photos,"total"=>$total,"links"=>&$Links) );
}


/*
 * Administration Area
 */

 
/*
 *	
 * Albums
 *------------------------------------------------------------------------------
 */ 
 
public function addAlbum(){

if( $this->System->checkPOST("name","title","albumComment","photoComment","Submit") && isset($_FILES["image"]) ) {

list($name,$title,$albumComment,$photoComment) = $this->System->escapePOST("name","title","albumComment","photoComment");

if(!empty($name)){

/*
 * Image coping, resizing block
 */

$image=$this->System->file->copyFile("image",array("jpg","jpeg","gif","png"));

if($image){

$small=$this->System->file->cp($image);

if($small){

# resizing

if(!$this->System->file->resizeImage($image,$this->largeX,$this->largeY)){
print $this->System->errors->Message($this->System->file->lastError);
} else {

if(!$this->System->file->resizeImage($small,$this->smallX,$this->smallY)){
print $this->System->errors->Message($this->System->file->lastError);
} else {
# Another code

$this->System->dbo->insert($this->AlbumTable,array("name"=>$name,"comment"=>$albumComment,"created"=>date("Y-m-d H:i:s"),
												   "owner"=>$this->System->user->GetUserID() ));

$albumID = $this->System->dbo->select($this->AlbumTable,"album_id","order by album_id desc limit 1 offset 0",_RESULT_ROW);

$albumID = $albumID["album_id"];
	
$this->System->dbo->insert($this->PhotosTable,array("title"=>$title,"album_id"=>$albumID,"created"=>date("Y-m-d H:i:s"),
													"small"=>$small,"large"=>$image,"comment"=>$photoComment,"rating"=>0));
													
$this->System->base->sendHeader("gallery-managealbums");
# End of Another Code
}

}


}
else {
print $this->System->errors->Message($this->System->file->lastError);
}

}
else {
print $this->System->errors->Message($this->System->file->lastError);
}

/*
 * End
 */ 

}
else {
print $this->System->errors->Message($this->lang->get("g8"));
}

}

return $this->System->view->GetTemplate("gallery","newalbum");
}

public function manageAlbum(){

if(!isset($_GET["album"]) || !is_numeric($_GET["album"]) )
	return $this->System->errors->Message("g13");

$album = $this->System->escapeString($_GET["album"]);

$this->System->base->AddWay("gallery-managealbums",null,$this->lang->get("g1"));

$AlbumData = $this->System->dbo->select($this->AlbumTable,"name,created"," where album_id='$album' limit 1 offset 0",_RESULT_ROW);

if(count($AlbumData)==0){

return $this->System->errors->Message($this->lang->get("g20"));
}

$Query = $this->System->dbo->select($this->PhotosTable." as a, file as b","a.title,a.photo_id,a.small,a.created,a.comment,
a.rating,b.size,b.name","
where a.album_id='".$album."' and b.id=a.small limit 200 offset 0");	

# getting small images
$i=0;
while($i<count($Query)){

$Query[$i]["small"]=$this->System->file->GetSRC($Query[$i]["name"]);

$i++;
}

$this->System->base->AddWay("gallery-managealbum","album=".$album,$this->lang->get("g18")." \"".$AlbumData["name"]."\"");

# making a template

$this->System->base->setHTMLHeader("js","gallery");

return $this->System->view->GetTemplate("gallery","albumAdmin",array("count"=>count($Query),"data"=>&$Query,"album"=>$album,"AlbumData"=>&$AlbumData) );	
}

/*
 * -----------------------------------------------------------------------------
 * Albums Managing
 */


public function manageAlbums(){

$Query = $this->System->dbo->select($this->AlbumTable." as a,".$this->PhotosTable." as b","
a.album_id,a.name,a.created,a.comment,b.small","
where b.album_id=a.album_id group by a.album_id limit 100 offset 0");

if(count($Query)==0){

return $this->System->errors->Message($this->lang->get("g2"));
}

$Images=array();

$i=0;
while($i<count($Query)){

$Images[$Query[$i]["album_id"]]=$this->System->file->GetLink($Query[$i]["small"]);

$i++;
}

return $this->System->view->GetTemplate("gallery","manage",array("data"=>&$Query,"count"=>count($Query),"image"=>&$Images)  );
}


public function ajaxtools(){

if(isset($_GET["Album"]) && is_numeric($_GET["Album"]) && isset($_GET["Value"]) && strlen($_GET["Value"])>0){

$value=$this->System->escapeString($_GET["Value"]);

$Album=$this->System->escapeString($_GET["Album"]);

$this->System->dbo->update($this->AlbumTable,"set name='$value' where album_id='$Album'");

}

}

public function deletealbum(){

if(isset($_GET["album"]) && is_numeric($_GET["album"])){

$album=$this->System->escapeString($_GET["album"]);

$Images=$this->System->dbo->select($this->PhotosTable,"small,large","where album_id='$album'");

$i=0;
while($i<count($Images)){

$this->System->file->delete($Images[$i]["small"]);

$this->System->file->delete($Images[$i]["large"]);

$i++;
}

$this->System->dbo->delete($this->PhotosTable,"where album_id='".$album."'");

$this->System->dbo->delete($this->AlbumTable," where album_id='".$album."'");

$this->System->base->sendHeader("gallery-managealbums",null);
}
else return $this->System->errors->Message($this->lang->get("g20"));


}

/*
 *	
 * Photos
 *------------------------------------------------------------------------------
 */ 

public function addPhoto(){

$this->System->base->AddWay("gallery-managealbums",null,$this->lang->get("g1"));

if(!isset($_GET["album"]) || !is_numeric($_GET["album"]))
return $this->System->errors->Message($this->lang->get("g13"));

$albumID=$this->System->escapeString($_GET["album"]);

$Query=$this->System->dbo->select($this->AlbumTable,"name","where album_id='$albumID' limit 1 offset 0",_RESULT_ROW);

if(count($Query)==0)
return $this->System->errors->Message("g20");

$this->System->base->AddWay("gallery-managealbum","album=".$albumID,$this->lang->get("g18")." \"".$Query["name"]."\"");

$this->System->base->AddWay("gallery-addphoto","album=".$albumID,$this->lang->get("g25"));

if($this->System->checkPOST("title","comment") && isset($_FILES["image"]) ){

list($title,$comment) = $this->System->escapePOST("title","comment");

$image=$this->System->file->copyFile("image",array("jpg","jpeg","gif","png"));

if($image){

$small=$this->System->file->cp($image);

if($small){

# resizing

if(!$this->System->file->resizeImage($image,$this->largeX,$this->largeY)){
print $this->System->errors->Message($this->System->file->lastError);
} else {

if(!$this->System->file->resizeImage($small,$this->smallX,$this->smallY)){
print $this->System->errors->Message($this->System->file->lastError);
} else {
# Another code
	
$this->System->dbo->insert($this->PhotosTable,array("title"=>$title,"album_id"=>$albumID,"created"=>date("Y-m-d H:i:s"),
													"small"=>$small,"large"=>$image,"comment"=>$comment,"rating"=>0));
													
$this->System->base->sendHeader("gallery-managealbum","album=".$albumID);
# End of Another Code
}

}


}
else {
print $this->System->errors->Message($this->System->file->lastError);
}

}
else {
print $this->System->errors->Message($this->System->file->lastError);
}



}



return $this->System->view->GetTemplate("gallery","addphoto",array("name"=>$Query["name"]) );
} 
 
 
public function editphoto(){

if(isset($_GET["photo"]) && is_numeric($_GET["photo"])){

$photoID = $this->System->escapeString($_GET["photo"]);

if($this->System->checkPOST("title","Submit","comment")){

list($name,$comment)=$this->System->escapePOST("title","comment");

$this->System->dbo->update($this->PhotosTable," set title='".$name."',comment='$comment' where photo_id='$photoID'");

}

$this->System->base->AddWay("gallery-managealbums",null,$this->lang->get("g1"));

$Photo= $this->System->dbo->select($this->PhotosTable,"large,album_id,title,comment"," where photo_id='".$photoID."' 
	limit 1 offset 0",_RESULT_ROW);

if(count($Photo)==0){
return $this->System->errors->Message($this->lang->get("g17"));
}	
	
$Album = $this->System->dbo->select($this->AlbumTable,"name","where album_id='".$Photo["album_id"]."'",_RESULT_ROW);	

$this->System->base->AddWay("gallery-managealbum","album=".$Photo["album_id"],$this->lang->get("g18")." \"".$Album["name"]."\"");

$this->System->base->AddWay("gallery-editphoto","photo=".$photoID,$this->lang->get("g19"));

$image = $this->System->file->GetLink($Photo["large"]);
	
return $this->System->view->GetTemplate("gallery","editPhoto",array(
	"image"=>$Photo,
	"album"=>$Album["name"],
	"photo"=>$image,
	"id"   =>$photoID
));


}
else {
return $this->System->errors->Message($this->lang->get("g17"));
}
 
}


#photoDeleting
public function deletephoto(){
 
if(isset($_GET["photo"]) && is_numeric($_GET["photo"])){

$id=$this->System->escapeString($_GET["photo"]);

$Query=$this->System->dbo->select($this->PhotosTable,"album_id,small,large","where photo_id='$id' limit 1 offset 0",_RESULT_ROW);

if(count($Query)==0) return $this->System->errors->Message($this->lang->get("g17"));

$this->System->file->delete($Query["small"]);

$this->System->file->delete($Query["large"]);

$this->System->dbo->delete($this->PhotosTable," where photo_id='$id'");

$Count=$this->System->dbo->select($this->PhotosTable,"COUNT(*) as count"," where album_id='".$Query["album_id"]."'",_RESULT_ROW);

if( (int)$Count["count"]==0){

$this->System->dbo->delete($this->AlbumTable," where album_id='".$Query["album_id"]."'");

$this->System->base->sendHeader("gallery-managealbums",null);
}

$this->System->base->sendHeader("gallery-managealbum","album=".$Query["album_id"]);

}else return $this->System->errors->Message($this->lang->get("g17"));
 

}

/*
 * Module Options
 */

public function options(){



}

public function install(){

$AlbumTable["name"] = $this->AlbumTable;
$AlbumTable["field"][]=array("name"=>"album_id","type"=>"int_auto");
$AlbumTable["field"][]=array("name"=>"name","type"=>"char");
$AlbumTable["field"][]=array("name"=>"created","type"=>"datetime");
$AlbumTable["field"][]=array("name"=>"owner","type"=>"int");
$AlbumTable["field"][]=array("name"=>"comment","type"=>"text");
$AlbumTable["pkey"][]=array("field"=>"album_id");
$AlbumTable["key"][]=array("field"=>"name");
$this->System->dbo->createtable($AlbumTable);

$PhotosTable["name"] = $this->PhotosTable;
$PhotosTable["field"][]=array("name"=>"photo_id","type"=>"int_auto");
$PhotosTable["field"][]=array("name"=>"album_id","type"=>"int");
$PhotosTable["field"][]=array("name"=>"title","type"=>"char");
$PhotosTable["field"][]=array("name"=>"small","type"=>"int");
$PhotosTable["field"][]=array("name"=>"large","type"=>"int");
$PhotosTable["field"][]=array("name"=>"created","type"=>"datetime");
$PhotosTable["field"][]=array("name"=>"comment","type"=>"text");
$PhotosTable["field"][]=array("name"=>"rating","type"=>"int");
$PhotosTable["pkey"][]=array("field"=>"photo_id");
$PhotosTable["key"][]=array("field"=>"album_id");
$this->System->dbo->createtable($PhotosTable);

$this->System->errors->Message($this->lang->get("g12"));
}

public function uninstall(){

$this->System->dbo->direct("drop table if exists ".$this->AlbumTable);

$this->System->dbo->direct("drop table if exists ".$this->PhotosTable);

}

}
?>
